import sys
import os
grandparent_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(grandparent_dir)

import requests
from utils.tools import read_json

def location(TK, keyWord, specify):
    base_url = "http://api.tianditu.gov.cn/v2/search"
    postStr = """
        {
            \"keyWord\" : \"%s\",
            \"specify\" : \"%s\",
            \"level\" : 12,
            \"queryType\" : 1,
            \"start\" : 0,
            \"count\" : 5,
            \"mapBound\": \"-180,-90,180,90\"
        }
    """ %(keyWord, specify)
    url = f"{base_url}?postStr={postStr}&type=query&tk={TK}"
    url = url.replace("\n", "").replace(" ", "")
    response = requests.get(url)
    return response.json()

if __name__ == "__main__":
    config = read_json("path_planning/tianditu/config.jsonc")
    TK = config["TK"]
    names = ["富力院士廷A区",
             "两江新区医院", 
             "重庆医科大学附属第一医院",
             "大渡口区医院",
             "重庆医科大学附属璧山医院",
             "沙坪坝区中西结合医院",
             "璧山医院",
             "九龙坡区医院"]
    specify = "重庆市"
    for keyWord in names:
        result = location(TK, keyWord, specify)
        print (result)
